package com.hxzy.bbs.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hxzy.jsp.dao.PostDAO;
import com.hxzy.jsp.entity.Post;
import com.hxzy.jsp.utils.Page;
import com.hxzy.test.PostDaoHibernateImpl;

public class IndexServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public IndexServlet() {
		super();
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PostDAO postDAO = new PostDaoHibernateImpl();
		int p = 1;
		try {
			p = Integer.parseInt(request.getParameter("p"));
		} catch (NumberFormatException e) {
			p = 1;
		}
		Page pager = new Page(postDAO.count(), 5, p);
		List<Post> postList = postDAO.findAll(pager.getOffset(), pager.getPageSize());
		// 把数据保存在 request 作用域里，传递到 jsp 页面用于渲染
		request.setAttribute("postList", postList);
		request.setAttribute("pager", pager);

		request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
